#!/bin/bash
# Program:
#   v1.0: 查看机器的序列号、超线程、服务器型号、核心数量 等信息

export PATH=/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin

#序列号
serialNumber=$(dmidecode -t 1|grep "Serial Number"|awk -F':' '{print $2}')
# 超线程
threadsNum=$(lscpu |grep 'Thread(s) per core'|awk '{print $4}')
#服务器型号
productName=$(dmidecode -t 1|grep "Product Name"|awk -F':' '{print $2}')
# 核心
#Core_Num=$(lscpu | grep 'CPU(s):' | head -1 | awk '{print $2}')

cpuNums=$(cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c|sed 's/^[ ]*//g')
cpuPhysical=$(cat /proc/cpuinfo| grep "physical id"|sort -n|uniq|wc -l)
cpuCores=$(cat /proc/cpuinfo| grep "cpu cores"| uniq|awk '{print $NF}')
cpuInfo="总核心:$cpuNums [${cpuPhysical}*${cpuCores}]"

totalMem=0
totalCards=$(dmidecode|grep -P -A5 "Memory\s+Device"|grep Size|grep -v Range|wc -l)
usedCards=$(dmidecode|grep -P -A5 "Memory\s+Device"|grep Size|grep -v Range|grep -v "No Module"|wc -l)
memLists=()
oldIFS=$IFS
IFS=$'\n' #换行符作为分隔符
if [[ $threadsNum -ne 1 ]] ;then
	Flag="已开超线程CPU[$threadsNum]"
fi
for i in $(dmidecode|grep -P -A5 "Memory\s+Device"|grep Size|grep -v Range|grep -v "No Module"|awk '{print $2,$3}') ;do
    value=$(echo $i|awk '{print $1}')
    flag=$(echo $i|awk '{print $2}')
    if [[ "$flag" == "MB" ]] ;then
        value=$((${value}/1024))
	fi
    totalMem=$(($totalMem+$value))
    memLists+=($value)
done
IFS=$oldIFS # 恢复默认值
 
memInfo="${totalMem}GB[卡槽 ${usedCards}/${totalCards}]"

rootSize=$(df -h /|tail -1|awk '{print $2}')
homeSize=$(df -h /home|tail -1|awk '{print $2}')
diskInfo="[/目录]: ${rootSize} [home目录]: ${homeSize}"
    
echo "${Flag} ${serialNumber},${productName},${cpuInfo},${memInfo},${diskInfo}"
#echo '[总核心] = 物理CPU数量 * 单个核心数量 * 线程数量 (如果有2个物理CPU, 单个CPU是32核,总共是128核心,那说明2个物理CPU有线程技术)'


